jQuery(function() {
    var tabPanel = new SqlPlusPanel({"container": "db-list-panel"});

    jQuery("#db-list tr td a.rename").click(function() {
        var params = [];
        var name = PageContext.getAttribute("connectionName");
        var oldDatabase = PageContext.getAttribute("database");
        var newDatabase = PageContext.getAttribute("newDatabase");
        params[params.length] = "name=" + encodeURIComponent(name);
        params[params.length] = "oldDatabase=" + encodeURIComponent(oldDatabase);
        params[params.length] = "newDatabase=" + encodeURIComponent(newDatabase);
        alert("rename: " + params.join("&"));
    });

    jQuery("#alter-panel .panel-title span.close").click(function() {
        jQuery("#alter-panel").hide();
    });

    jQuery("#alter-panel input.cancel").click(function() {
        jQuery("#alter-panel").hide();
    });

    jQuery("#alter-panel input.ensure").click(function() {
    });

    jQuery("#db-list tr td a.remove").click(function() {
        var database = jQuery(this).attr("database");
        jQuery("#alter-panel input.ensure").attr("database", database);
        AlterPanel.submit(Sql.highlight("-- drop database\r\ndrop database " + database + ";"));
    });
});

jQuery(function() {
    Dragable.registe("alter-panel");

    jQuery("#create-btn").click(function() {
        var params = PageContext.getParams(document.userForm);
        console.log(JSON.stringify(params));
        console.log(jQuery.param(params, true));

        jQuery.ajax({
            "type": "post",
            "url": PageContext.getContextPath() + "/webcat/oracle/tablespace/getCreateSql.html",
            "dataType": "json",
            "data": jQuery.param(params, true),
            "error": function() {
                AlterPanel.error("系统错误，请稍后再试！");
                AlterPanel.setEnabled(true);
            },
            "success": function(result) {
                if(result != null) {
                    jQuery("#alter-panel input.cancel").unbind();
                    jQuery("#alter-panel input.cancel").click(function() {
                        AlterPanel.close();
                    });
                    AlterPanel.show(Sql.highlight(result.value));
                }
                else {
                    AlterPanel.error("系统错误，请稍后再试！");
                    AlterPanel.setEnabled(true);
                }
            }
        });
    });
});

jQuery(function() {
    jQuery("#db-list tr").mouseover(function() {
        this.style.backgroundColor = "#ffda5a";
    });

    jQuery("#db-list tr").mouseout(function() {
        this.style.backgroundColor = "#ffffff";
    });
});

jQuery(function() {
    jQuery(window).trigger("resize");
});

jQuery(function() {
    jQuery("input[name=tableSpaceName]").change(function() {
        var tableSpaceName = jQuery.trim(this.value);

        if(tableSpaceName.length < 1) {
            return;
        }

        var dataFile = jQuery("input[name=dataFile]").val();
        dataFile = dataFile.replace(/\\/g, "/");

        var k = dataFile.lastIndexOf("/");

        if(k > -1) {
            jQuery("input[name=dataFile]").val(dataFile.substring(0, k) + "/" + tableSpaceName + ".dbf");
        }
        else {
            jQuery("input[name=dataFile]").val(tableSpaceName + ".dbf");
        }
    });

    jQuery("input[name=autoExtend]").change(function() {
        if(this.checked == true) {
            jQuery("input[name=nextExtend]").prop("disabled", false);
            jQuery("input[name=maxSize]").prop("disabled", false);
        }
        else {
            jQuery("input[name=nextExtend]").prop("disabled", true);
            jQuery("input[name=maxSize]").prop("disabled", true);
        }
    });
    jQuery("input[name=tableSpaceName]").change();
    jQuery("input[name=autoExtend]").change();
});

